/*
This file is part of asproxy.

asproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

asproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with asproxy.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "definitions.h"
#include "proxyconfigfile.h"
#include "proxydaemon.h"

int start_daemons()
{
	PROXYCONFIG** proxycollection = (PROXYCONFIG**)malloc(0 * sizeof(PROXYCONFIG*));
	int length = 0;
	proxycollection = read_configfile(proxycollection, &length);

	if (length == 0)
		return 0;

	int i = 0;
	int result = 0;
	for (; i < length; i++)
	{
		// Start the individual daemon
		result = start_daemon(length, proxycollection[i]);				
	}	

	// Clear the collection once all the values have been read.
	free_proxycollection(length, proxycollection);
	
	return result;
}

int main(int argc, char** argv)
{
	if ((argc > 1) && (strcmp(argv[1], "-s") == 0))
	{
		start_daemons();
		return 0;
	}
	else if ((argc > 1) && (strcmp(argv[1], "-k") == 0))
	{
		stop_daemons();
		return 0;
	}
	
	char resp;
	
	// Startup
	printf("\n*** Welcome to asproxy *** \n");
	
	while (1)
	{
		printf("\nPlease choose from the following: \n");
		printf("     s: Start the asproxy daemons.\n");
		printf("     k: Kill asproxy daemons.\n");
		printf("     r: Review asproxy.config.\n");
		printf("     m: Monitor Daemons.\n");
		printf("     q: Quit.\n");
		printf("Please make your selection (s,k,r,m,q):");
		
		char c;
		while ((c = getchar()) != '\n')
			resp = c;
		
		// Get the input
		switch(resp)
		{
			case 's': 
			{
				int result = start_daemons();
				if (result <= 0)
					return 0;
					
				break;
			}
			case 'k':
			{
				stop_daemons();
				break;
			}
			case 'r':
			{
				// Read the Configs
				PROXYCONFIG** proxycollection = (PROXYCONFIG**)malloc(0 * sizeof(PROXYCONFIG*));
				int length = 0;
				proxycollection = read_configfile(proxycollection, &length);

				int nLength = 0;
				for (; nLength < length; nLength++)
				{
					printf("Listener: %s:%d, Destination: %s:%d, Source Format: %s, Cert: %s, CA Path: %s, BufferSize: %d\n", 
						proxycollection[nLength]->ListeningIP, proxycollection[nLength]->ListeningPort, 
						proxycollection[nLength]->DestinationIP, proxycollection[nLength]->DestinationPort, 
						proxycollection[nLength]->SourceDetailFormat, proxycollection[nLength]->SSLCertFile, 
						proxycollection[nLength]->SSLCAPath, proxycollection[nLength]->BufferSize);	
				}		

				// Clear the collection once all the values have been read.
				free_proxycollection(length, proxycollection);
									
				break;
			}
			case 'm':
			{
				show_daemoninfo();
				break;
			}
			case 'q':
				printf("\n*** Ending asproxy parent session ***\n");
				return 0;
			default:
				printf("\nBad selection. Please choose from the list (case sensitive)\n");
				break;
		};
	}
}
